package com.summer.safe.controller.system;

import com.summer.safe.base.BaseController;
import com.summer.safe.base.TreeNode;
import com.summer.safe.config.annotation.DataFilter;
import com.summer.safe.config.annotation.ResultWrapper;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.system.entity.Office;
import com.summer.safe.system.entity.User;
import com.summer.safe.system.service.OfficeService;
import com.summer.safe.utils.*;
import com.summer.safe.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门Controller
 *
 */
@Controller
@RequestMapping(value = "/sys/office")
public class OfficeController extends BaseController {

    private String PREFIX = "/system/office/";

    @Autowired
    private OfficeService officeService;

    @ModelAttribute("office")
    public Office get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return officeService.get(id);
        } else {
            return new Office();
        }
    }

    @RequiresPermissions("sys:office:view")
    @GetMapping("")
    public String index() {
        return PREFIX + "officelist.html";
    }

    @GetMapping("/officeData")
    @ResultWrapper(ResultCode.SUCCESS_QUERY)
    public List<Office> list(Office office) {
        List<Office> list = officeService.findAll();
        return list;
    }

    @RequiresPermissions("sys:office:edit")
    @GetMapping("/form")
    public String form(Office office, Model model) {
        if (office.getParent() == null || office.getParent().getId() == null) {
            office.setParent(UserUtils.getUser().getOffice());
        }
        office.setParent(officeService.get(office.getParent().getId()));
        if (office.getArea() == null) {
            office.setArea(UserUtils.getUser().getOffice().getArea());
        }

        model.addAttribute("office", office);
        return PREFIX + "officeform.html";
    }



    /**
     * 获取部门JSON数据。
     *
     * @param isAll
     * @return
     */
    @ResponseBody
    @GetMapping(value = "treeData")
    @DataFilter(type = Global.OFFICE_DATE)
    public List<TreeNode> treeData(Boolean isAll, Office office) {
        User user=UserUtils.getUser();
        CacheUtils.put(user.getId(),office.getSqlMap());
        List<Office> list = officeService.findList(isAll);

        return TreeUtils.treeNodes(list);
    }


    @PostMapping(value = "delete")
    @ResultWrapper(ResultCode.SUCCESS_DELETE)
    public void delete(Office office) {
        officeService.delete(office);
    }

    @PostMapping(value = "save")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public void save(Office office) {
        //判断是否为演示模式
        ToolUtil.DemoMode();
        officeService.save(office);
    }
}
